Apply for Zend Framework Certification Training

MongoDb






we will learn about updating the first occurrence of data matching
a certain criteria from a mongodb collection using node.js
.

//Query parameter is used to search the collection.
	var query = { name : "rajesh" };
	//And When the query matches the data in the DB , "data" parameter is used to update the value.
	var data = { name : "rajesh@gmail.com" , mobile : "6655443322" }
	//Accessing the collection using nodejs
	db.collection("details")
		.updateOne(query , data, (err , collection) => {
			if(err) throw err;
			console.log("Record updated successfully");
			console.log(collection);
		});

we will learn about updating all the occurrences of data matching
a certain criteria from a mongodb collection using node.js .


//query store the search condition
	var query = { age : {$gt : "22" } };
	//data stores the updated value
	var data = { $set : {age : "above 22" } }
	//CREATING A COLLECTION IN MONGODB USING NODE.JS
	db.collection("company")
		.updateMany(query , data, (err , collection) => {
			if(err) throw err;
			console.log(collection.result.nModified + " Record(s) updated successfully");	
			//It will console the number of rows updated
			console.log(collection);
			db.close();
		});



< Uses of Projection in mongoDb with nodeJs Use of sort in mongoDb using nodeJs >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top